/**
 * 
 */
package v1c02;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**
 * @author jguan
 */
public class ImageViewer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				ImageViewerFrame imageViewerFrame = new ImageViewerFrame();
				imageViewerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				imageViewerFrame.setVisible(true);
			}
		});
	}
}

class ImageViewerFrame extends JFrame {
	/**
	 * 	
	 */
	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;

	public ImageViewerFrame() {
		setTitle("Hello Frame");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

		// use label to show image;
		final JLabel imageLabel = new JLabel();
		add(imageLabel);

		// create a jfilechooser to find file
		final JFileChooser fileChooser = new JFileChooser();
		fileChooser.setCurrentDirectory(new File("."));

		// create 1 menuBar 1 menu with 2 item;
		//

		JMenuBar menubar = new JMenuBar();
		setJMenuBar(menubar);

		JMenu fileMenu = new JMenu("File");
		menubar.add(fileMenu);

		JMenuItem openItem = new JMenuItem("Open");
		fileMenu.add(openItem);

		JMenuItem exitItem = new JMenuItem("Exit");
		fileMenu.add(exitItem);

		// create EventListener for menu items
		openItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int result = fileChooser.showOpenDialog(null);
				if (result == JFileChooser.APPROVE_OPTION) {
					String fileName = fileChooser.getSelectedFile().getPath();
					imageLabel.setIcon(new ImageIcon(fileName));
				}
			}
		});

		exitItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

	}
}